package com.huadao.cart_ccs.data.bean;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.huadao.cart_ccs.common.TimeUtil;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EnvironmentLog implements JsonDeserializer,JsonSerializer {
    private String deviceInfoId;
    private String projectId;
    private Double co2;
    private Double o2;
    private Double temperature;
    private Date deviceTime;
    private int state;
    private String processName;
    private String id;

    public static final int NORMAL=1;//正常
    public static final int ABNORMAL=2;//异常

    public String getDeviceInfoId() {
        return deviceInfoId;
    }

    public void setDeviceInfoId(String deviceInfoId) {
        this.deviceInfoId = deviceInfoId;
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public Double getCo2() {
        return co2;
    }

    public void setCo2(Double co2) {
        this.co2 = co2;
    }

    public Double getO2() {
        return o2;
    }

    public void setO2(Double o2) {
        this.o2 = o2;
    }

    public Double getTemperature() {
        return temperature;
    }

    public void setTemperature(Double temperature) {
        this.temperature = temperature;
    }

    public Date getDeviceTime() {
        return deviceTime;
    }

    public void setDeviceTime(Date deviceTime) {
        this.deviceTime = deviceTime;
    }

    public String getProcessName() {
        return processName;
    }

    public void setProcessName(String processName) {
        this.processName = processName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    @Override
    public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        EnvironmentLog environmentLog=new EnvironmentLog();
        JsonObject jsonObject=json.getAsJsonObject();
        if(jsonObject.has("deviceInfoId")){
            String deviceInfoId=jsonObject.get("deviceInfoId").getAsString();
            environmentLog.setDeviceInfoId(deviceInfoId);
        }
        if(jsonObject.has("projectId")){
            String projectId=jsonObject.get("projectId").getAsString();
            environmentLog.setProjectId(projectId);
        }
        if(jsonObject.has("co2")){
            Double co2=jsonObject.get("co2").getAsDouble();
            environmentLog.setCo2(co2);
        }
        if(jsonObject.has("o2")){
            Double o2=jsonObject.get("o2").getAsDouble();
            environmentLog.setO2(o2);
        }
        if(jsonObject.has("temperature")){
            Double temperature=jsonObject.get("temperature").getAsDouble();
            environmentLog.setTemperature(temperature);
        }
        if(jsonObject.has("deviceTime")){
            String deviceTimeStr=jsonObject.get("deviceTime").getAsString();
            try {
                Date deviceTime=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ").parse(deviceTimeStr);
                environmentLog.setDeviceTime(deviceTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if(jsonObject.has("state")){
            int state=jsonObject.get("state").getAsInt();
            environmentLog.setState(state);
        }
        if(jsonObject.has("processName")){
            String processName=jsonObject.get("processName").getAsString();
            environmentLog.setProcessName(processName);
        }
        if(jsonObject.has("id")){
            String id=jsonObject.get("id").getAsString();
            environmentLog.setId(id);
        }
        return environmentLog;
    }

    @Override
    public JsonElement serialize(Object src, Type typeOfSrc, JsonSerializationContext context) {
        if(src==null || !(src instanceof EnvironmentLog)){
            return null;
        }
        EnvironmentLog environmentLog= (EnvironmentLog) src;
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("deviceInfoId",environmentLog.getDeviceInfoId());
        jsonObject.addProperty("projectId",environmentLog.getProjectId());
        jsonObject.addProperty("co2",environmentLog.getCo2());
        jsonObject.addProperty("o2",environmentLog.getO2());
        jsonObject.addProperty("temperature",environmentLog.getTemperature());
        if(environmentLog.getDeviceTime()!=null){
            String deviceTimeStr=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(environmentLog.getDeviceTime());
            jsonObject.addProperty("deviceTime",deviceTimeStr);
        }
        jsonObject.addProperty("state",environmentLog.getState());
        jsonObject.addProperty("processName",environmentLog.getProcessName());
        jsonObject.addProperty("id",environmentLog.getId());
        return jsonObject;
    }

    @Override
    public String toString() {
        return "EnvironmentLog{" +
                "deviceInfoId='" + deviceInfoId + '\'' +
                ", projectId='" + projectId + '\'' +
                ", co2=" + co2 +
                ", o2=" + o2 +
                ", temperature=" + temperature +
                ", deviceTime=" + deviceTime +
                ", state=" + state +
                ", processName='" + processName + '\'' +
                ", id='" + id + '\'' +
                '}';
    }
}
